package com.cnlbc.View;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.cnlbc.Dao.BookDao;
import com.cnlbc.Dao.BookTypeDao;
import com.cnlbc.Model.Book;
import com.cnlbc.Model.BookType;
import com.cnlbc.Util.DbUtil;
import com.cnlbc.Util.StringUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;

public class ListBook extends JInternalFrame {
	private JTable bookTable;
	private JTextField sname;
	private JTextField sauthor;
	private JComboBox sbookType; 

	
	private DbUtil dbUtil=new DbUtil();
	private BookDao bookDao=new BookDao();
	private BookTypeDao bookTypeDao=new BookTypeDao();
	private JTextField id;
	private JTextField bookName;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField price;
	private JTextField author;
	private JRadioButton man;
	private JRadioButton women;
	private JTextArea bookDesc;
	private JComboBox bookType ;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ListBook frame = new ListBook();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ListBook() {
		setIconifiable(true);
		setClosable(true);
		setBounds(100, 100, 709, 546);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
							.addGap(33)
							.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 612, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap(36, Short.MAX_VALUE)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(scrollPane, Alignment.TRAILING)
								.addComponent(panel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE))))
					.addGap(39))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
					.addGap(13)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(26, Short.MAX_VALUE))
		);
		
		JLabel lblNewLabel_3 = new JLabel("编号：");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 16));
		
		id = new JTextField();
		id.setEditable(false);
		id.setText("");
		id.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("图书名称：");
		lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 16));
		
		bookName = new JTextField();
		bookName.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("作者性别：");
		lblNewLabel_5.setFont(new Font("宋体", Font.PLAIN, 16));
		
		man = new JRadioButton("男");
		man.setSelected(true);
		buttonGroup.add(man);
		
		women = new JRadioButton("女");
		buttonGroup.add(women);
		
		JLabel lblNewLabel_6 = new JLabel("价格：");
		lblNewLabel_6.setFont(new Font("宋体", Font.PLAIN, 16));
		
		price = new JTextField();
		price.setColumns(10);
		
		JLabel lblNewLabel_7 = new JLabel("作者：");
		lblNewLabel_7.setFont(new Font("宋体", Font.PLAIN, 16));
		
		author = new JTextField();
		author.setText("");
		author.setColumns(10);
		
		JLabel lblNewLabel_8 = new JLabel("图书类别：");
		lblNewLabel_8.setFont(new Font("宋体", Font.PLAIN, 16));
		
		bookType = new JComboBox();
		
		JLabel lblNewLabel_9 = new JLabel("图书描述：");
		lblNewLabel_9.setFont(new Font("宋体", Font.PLAIN, 16));
		
		bookDesc = new JTextArea();
		
		JButton btnNewButton_1 = new JButton("修改");
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 18));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateBookAction(e);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(ListBook.class.getResource("/images/modify.png")));
		
		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.setFont(new Font("宋体", Font.PLAIN, 18));
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deteleBookAction(e);
			}
		});
		btnNewButton_2.setIcon(new ImageIcon(ListBook.class.getResource("/images/delete.png")));
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_1.createSequentialGroup()
									.addComponent(lblNewLabel_3)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(id, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addComponent(lblNewLabel_6)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(price, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
							.addGap(27)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_4)
								.addComponent(lblNewLabel_7))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
								.addComponent(author)
								.addComponent(bookName, GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_5)
								.addComponent(lblNewLabel_8))
							.addGap(10)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_1.createSequentialGroup()
									.addComponent(man)
									.addGap(10)
									.addComponent(women))
								.addComponent(bookType, 0, 108, Short.MAX_VALUE)))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(lblNewLabel_9)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(bookDesc, GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE)))
					.addContainerGap())
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(159)
					.addComponent(btnNewButton_1)
					.addPreferredGap(ComponentPlacement.RELATED, 152, Short.MAX_VALUE)
					.addComponent(btnNewButton_2)
					.addGap(132))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
							.addComponent(bookName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblNewLabel_5)
							.addComponent(women)
							.addComponent(man))
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblNewLabel_3)
							.addComponent(id, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblNewLabel_4)))
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblNewLabel_6))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(2)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
								.addComponent(price, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_7)
								.addComponent(author, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_8)
								.addComponent(bookType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_9)
						.addComponent(bookDesc, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_1)
						.addComponent(btnNewButton_2))
					.addContainerGap())
		);
		panel_1.setLayout(gl_panel_1);
		
		JLabel lblNewLabel = new JLabel("图书名称：");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
		
		sname = new JTextField();
		sname.setFont(new Font("宋体", Font.PLAIN, 16));
		sname.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("作者：");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
		
		sauthor = new JTextField();
		sauthor.setFont(new Font("宋体", Font.PLAIN, 16));
		sauthor.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("图书类别：");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 16));
		
		sbookType = new JComboBox();
		sbookType.setFont(new Font("宋体", Font.PLAIN, 15));
		
		JButton btnNewButton = new JButton("搜索");
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 16));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SearchBookAction(e);
			}
		});
		btnNewButton.setIcon(new ImageIcon(ListBook.class.getResource("/images/search.png")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(sname, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(lblNewLabel_1)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(sauthor, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(lblNewLabel_2)
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(sbookType, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnNewButton)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(sname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2)
						.addComponent(sbookType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton)
						.addComponent(lblNewLabel_1)
						.addComponent(sauthor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		
		bookTable = new JTable();
		bookTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				bookTableMouseAction(e);
			}
		});
		bookTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u56FE\u4E66\u540D\u79F0", "\u4F5C\u8005", "\u4F5C\u8005\u6027\u522B", "\u56FE\u4E66\u4EF7\u683C", "\u56FE\u4E66\u7C7B\u522B", "\u56FE\u4E66\u63CF\u8FF0"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, true, false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		bookTable.getColumnModel().getColumn(0).setPreferredWidth(54);
		bookTable.getColumnModel().getColumn(1).setPreferredWidth(116);
		bookTable.getColumnModel().getColumn(2).setPreferredWidth(47);
		bookTable.getColumnModel().getColumn(5).setPreferredWidth(88);
		bookTable.getColumnModel().getColumn(6).setPreferredWidth(95);
		scrollPane.setViewportView(bookTable);
		getContentPane().setLayout(groupLayout);
		bookDesc.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
		
		this.fillBookType("search");
		this.fillBookType("modify"); 
		this.fillTable(new Book());
	}
	/**
	 * 修改图书信息
	 * @param e
	 */
	private void updateBookAction(ActionEvent e) {
		String id=this.id.getText();
		if(StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "请选择您要删除的数据");
			return;
		}
		String bookName=this.bookName.getText();
		String author=this.author.getText();
		String sex="";
		if(man.isSelected()) {
			sex="男";
		}else if(women.isSelected()) {
			sex="女";
		}
		String price=this.price.getText();
		if(StringUtil.isEmpty(bookName)) {
			JOptionPane.showMessageDialog(null, "图书名称不能为空");
			return;
		}
		if(StringUtil.isEmpty(author)) {
			JOptionPane.showMessageDialog(null, "图书作者不能为空");
			return;
		}
		if(StringUtil.isEmpty(price)) {
			JOptionPane.showMessageDialog(null, "图书价格不能为空");
			return;
		}
		BookType bookType=(BookType) this.bookType.getSelectedItem();
		int bookTypeid=bookType.getId();
		String bookDesc=this.bookDesc.getText();
		
		
		Connection con=null;
		Book book=new Book(Integer.parseInt(id),bookName,author,sex,Float.parseFloat(price), bookTypeid,bookDesc);
		try {
			con=dbUtil.getCon();
			int result=bookDao.updateBook(con, book);
			if(result==1) {
				JOptionPane.showMessageDialog(null, "修改成功");
				reset();
				this.fillTable(new Book());
			}else {
				JOptionPane.showMessageDialog(null, "修改失败");
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "修改失败");
		}finally {
			try {
				dbUtil.close(con);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 删除图书
	 * @param e
	 */
	private void deteleBookAction(ActionEvent e) {

		String id=this.id.getText();
		Connection con=null;
		try {
			con=dbUtil.getCon();
			int result=bookDao.deleteBook(con, id);
			if(result==1) {
				JOptionPane.showMessageDialog(null, "删除成功");
				reset();
				this.fillTable(new Book());
			}else {
				JOptionPane.showMessageDialog(null, "删除失败");
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "修改失败");
		}finally {
			try {
				dbUtil.close(con);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 表格点击事件
	 * @param evt
	 */
	private void bookTableMouseAction(MouseEvent met) {
		// TODO Auto-generated method stub
		int row=this.bookTable.getSelectedRow();
		int a=(int) bookTable.getValueAt(row,0);
		this.id.setText(a+""); 
		this.bookName.setText((String) bookTable.getValueAt(row, 1));
		this.author.setText((String)bookTable.getValueAt(row, 2));
		String sex=(String)bookTable.getValueAt(row, 3);
		if("男".equals(sex)) {
			this.man.setSelected(true);
		}else if("女".equals(sex)) {
			this.women.setSelected(true);
		}
		this.price.setText((Float)bookTable.getValueAt(row, 4)+"");
		String bookTypeName=(String)this.bookTable.getValueAt(row, 5);
		int n=this.bookType.getItemCount();
		for(int i=0;i<n;i++) {
			BookType item=(BookType)this.bookType.getItemAt(i);
			if(item.getBookTypeName().equals(bookTypeName)) {
				this.bookType.setSelectedIndex(i);
			}
		}
		this.bookDesc.setText((String) bookTable.getValueAt(row, 6));
	}

	/**
	 * 查询图书
	 * @param evt
	 */
	private void SearchBookAction(ActionEvent evt) {
		String bookName=this.sname.getText();
		String Author=this.sauthor.getText();
		BookType bookType=(BookType) this.sbookType.getSelectedItem();
		int bookTypeid=bookType.getId();
		
		Book book=new Book(bookName, Author, bookTypeid);
		this.fillTable(book);		
	}

	/**
	 * 初始化下拉框
	 * @param type
	 */	 
	private void fillBookType(String type) {
		Connection con=null;
		BookType bookType=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs=bookTypeDao.list(con, new BookType());
			if("search".equals(type)) {
				bookType=new BookType();
			    bookType.setBookTypeName("请选择"); 
			    bookType.setId(-1);
			  this.sbookType.addItem(bookType); 
			}
			while(rs.next()) {
				bookType=new BookType();
				bookType.setId(rs.getInt("id"));
				bookType.setBookTypeName(rs.getString("bookTypeName"));
				if("search".equals(type)) {
					this.sbookType.addItem(bookType); 
			  }else if("modify".equals(type)) {
				  	this.bookType.addItem(bookType);
			  } 
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbUtil.close(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
	 * 初始化表格数据
	 * @param book
	 */
	private void fillTable(Book book) {
		DefaultTableModel dtm=(DefaultTableModel) bookTable.getModel();
		dtm.setRowCount(0);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs=bookDao.ListBook(con,book);
			while(rs.next()) {
				Vector v=new Vector();
				v.add(rs.getInt("id"));
				v.add(rs.getString("bookName"));
				v.add(rs.getString("author"));
				v.add(rs.getString("sex"));
				v.add(rs.getFloat("price"));
				v.add(rs.getString("bookTypeName"));
				v.add(rs.getString("bookDesc"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbUtil.close(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private void reset() {
		this.id.setText("");
		this.bookName.setText("");
		this.author.setText("");
		this.man.setSelected(true);
		this.price.setText("");
		this.bookDesc.setText("");
		if(this.bookType.getItemCount()>0) {
			this.bookType.setSelectedIndex(0);
		}
	}
}
	